﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace mevsukovGames
{
    class ConsoleRenderer
    {
        private int startPointX;
        private int startPointY;

        public ConsoleRenderer()
        {
            startPointX = 0;
            startPointY = 0;
            Console.CursorVisible = false;
        }

        public ConsoleRenderer(int x, int y)
        {
            startPointX = x;
            startPointY = y;
            Console.CursorVisible = false;
        }

        public void ClearConsole()
        {
            Console.Clear();
        }

        public void RenderGameField(GameField field, CoordinateCalculator calc)

        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.SetCursorPosition(startPointX + 1, startPointY + 1);
            for (int i = startPointY + 1; i <= startPointY + field.Height; i++)
            {
                for (int j = startPointX + 1; j <= startPointX + field.Width; j++)
                {
                    if (!field.IsPositionFree(calc.ConsoleXToGameField(j), calc.ConsoleYToGameField(i)))
                    {
                        Console.SetCursorPosition(j, i);
                        Console.Write("■");
                    }
                    else
                    {
                        Console.SetCursorPosition(j, i);
                        Console.Write(" ");
                    }
                }
            }
        }

        public void RenderFrames(GameField field)
        {
            Console.SetCursorPosition(startPointX, startPointY);
            Console.Write("╔");
            for (int i = startPointX + 1; i <= startPointX + field.Width; i++)
            {
                Console.SetCursorPosition(i, startPointY);
                Console.Write("═");
            }
            Console.SetCursorPosition(startPointX + field.Width+1, startPointY);
            Console.Write("╗");
            for (int i = startPointY + 1; i <= startPointY + field.Height; i++)
            {
                Console.SetCursorPosition(startPointX+field.Width+1, i);
                Console.Write("║");
            }
            Console.SetCursorPosition(startPointX, startPointY + field.Height+1);
            Console.Write("╚");
            for (int i = startPointX + 1; i <= startPointX + field.Width; i++)
            {
                Console.SetCursorPosition(i, startPointY+field.Height+1);
                Console.Write("═");
            }
            Console.SetCursorPosition(startPointX + field.Width+1, startPointY + field.Height+1);
            Console.Write("╝");
            for (int i = startPointY + field.Height; i > startPointY; i--)
            {
                Console.SetCursorPosition(startPointX, i);
                Console.Write("║");
            }
        }

        public void RenderFieldShape(Shape figure, GameField field, CoordinateCalculator calc)
        {
            if(field.IsFieldContains(figure.FirstBlock.BlockCoordinates.XCoordinate, figure.FirstBlock.BlockCoordinates.YCoordinate))
            {
                Console.ForegroundColor = figure.FirstBlock.Color;
                Console.SetCursorPosition(calc.GameFieldXToConsole(figure.FirstBlock.BlockCoordinates.XCoordinate), calc.GameFieldYToConsole(figure.FirstBlock.BlockCoordinates.YCoordinate));
                Console.Write("■");
            }
            if (field.IsFieldContains(figure.SecondBlock.BlockCoordinates.XCoordinate, figure.SecondBlock.BlockCoordinates.YCoordinate))
            {
                Console.ForegroundColor = figure.SecondBlock.Color;
                Console.SetCursorPosition(calc.GameFieldXToConsole(figure.SecondBlock.BlockCoordinates.XCoordinate), calc.GameFieldYToConsole(figure.SecondBlock.BlockCoordinates.YCoordinate));
                Console.Write("■");
            }
            if (field.IsFieldContains(figure.ThirdBlock.BlockCoordinates.XCoordinate, figure.ThirdBlock.BlockCoordinates.YCoordinate))
            {
                Console.ForegroundColor = figure.ThirdBlock.Color;
                Console.SetCursorPosition(calc.GameFieldXToConsole(figure.ThirdBlock.BlockCoordinates.XCoordinate), calc.GameFieldYToConsole(figure.ThirdBlock.BlockCoordinates.YCoordinate));
                Console.Write("■");
            }
            if (field.IsFieldContains(figure.FourthBlock.BlockCoordinates.XCoordinate, figure.FourthBlock.BlockCoordinates.YCoordinate))
            {
                Console.ForegroundColor = figure.FourthBlock.Color;
                Console.SetCursorPosition(calc.GameFieldXToConsole(figure.FourthBlock.BlockCoordinates.XCoordinate), calc.GameFieldYToConsole(figure.FourthBlock.BlockCoordinates.YCoordinate));
                Console.Write("■");
            }

        }

        public void RenderNextShape(Shape figure, int xShift, int yShift, CoordinateCalculator calc)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(calc.GameFieldXToConsole(0 + xShift), calc.GameFieldYToConsole(0));
            Console.Write("Next shape:");
            Console.ForegroundColor = figure.FirstBlock.Color;
            Console.SetCursorPosition(calc.GameFieldXToConsole(figure.FirstBlock.BlockCoordinates.XCoordinate + xShift), calc.GameFieldYToConsole(figure.FirstBlock.BlockCoordinates.YCoordinate + yShift));
            Console.Write("■");
            Console.SetCursorPosition(calc.GameFieldXToConsole(figure.SecondBlock.BlockCoordinates.XCoordinate + xShift), calc.GameFieldYToConsole(figure.SecondBlock.BlockCoordinates.YCoordinate + yShift));
            Console.Write("■");
            Console.SetCursorPosition(calc.GameFieldXToConsole(figure.ThirdBlock.BlockCoordinates.XCoordinate + xShift), calc.GameFieldYToConsole(figure.ThirdBlock.BlockCoordinates.YCoordinate + yShift));
            Console.Write("■");
            Console.SetCursorPosition(calc.GameFieldXToConsole(figure.FourthBlock.BlockCoordinates.XCoordinate + xShift), calc.GameFieldYToConsole(figure.FourthBlock.BlockCoordinates.YCoordinate + yShift));
            Console.Write("■");
        }

        public void RenderScore(int score, int xShift, int yShift, CoordinateCalculator calc)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(calc.GameFieldXToConsole(0 + xShift), calc.GameFieldYToConsole(5));
            Console.Write("Game Score: {0}", score);
        }

        public void ClearFieldShape(Shape figure, GameField field, CoordinateCalculator calc)
        {
            if (field.IsFieldContains(figure.FirstBlock.BlockCoordinates.XCoordinate, figure.FirstBlock.BlockCoordinates.YCoordinate))
            {
                Console.SetCursorPosition(calc.GameFieldXToConsole(figure.FirstBlock.BlockCoordinates.XCoordinate), calc.GameFieldYToConsole(figure.FirstBlock.BlockCoordinates.YCoordinate));
                Console.Write(" ");
            }
            if (field.IsFieldContains(figure.SecondBlock.BlockCoordinates.XCoordinate, figure.SecondBlock.BlockCoordinates.YCoordinate))
            {
                Console.SetCursorPosition(calc.GameFieldXToConsole(figure.SecondBlock.BlockCoordinates.XCoordinate), calc.GameFieldYToConsole(figure.SecondBlock.BlockCoordinates.YCoordinate));
                Console.Write(" ");
            }
            if (field.IsFieldContains(figure.ThirdBlock.BlockCoordinates.XCoordinate, figure.ThirdBlock.BlockCoordinates.YCoordinate))
            {
                Console.SetCursorPosition(calc.GameFieldXToConsole(figure.ThirdBlock.BlockCoordinates.XCoordinate), calc.GameFieldYToConsole(figure.ThirdBlock.BlockCoordinates.YCoordinate));
                Console.Write(" ");
            }
            if (field.IsFieldContains(figure.FourthBlock.BlockCoordinates.XCoordinate, figure.FourthBlock.BlockCoordinates.YCoordinate))
            {
                Console.SetCursorPosition(calc.GameFieldXToConsole(figure.FourthBlock.BlockCoordinates.XCoordinate), calc.GameFieldYToConsole(figure.FourthBlock.BlockCoordinates.YCoordinate));
                Console.Write(" ");
            }
        }

        public void ClearNextShape(Shape figure, int xShift, int yShift, CoordinateCalculator calc)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(calc.GameFieldXToConsole(0 + xShift), calc.GameFieldYToConsole(0));
            Console.Write("Next shape:");
            Console.SetCursorPosition(calc.GameFieldXToConsole(figure.FirstBlock.BlockCoordinates.XCoordinate + xShift), calc.GameFieldYToConsole(figure.FirstBlock.BlockCoordinates.YCoordinate + yShift));
            Console.Write(" ");
            Console.SetCursorPosition(calc.GameFieldXToConsole(figure.SecondBlock.BlockCoordinates.XCoordinate + xShift), calc.GameFieldYToConsole(figure.SecondBlock.BlockCoordinates.YCoordinate + yShift));
            Console.Write(" ");
            Console.SetCursorPosition(calc.GameFieldXToConsole(figure.ThirdBlock.BlockCoordinates.XCoordinate + xShift), calc.GameFieldYToConsole(figure.ThirdBlock.BlockCoordinates.YCoordinate + yShift));
            Console.Write(" ");
            Console.SetCursorPosition(calc.GameFieldXToConsole(figure.FourthBlock.BlockCoordinates.XCoordinate + xShift), calc.GameFieldYToConsole(figure.FourthBlock.BlockCoordinates.YCoordinate + yShift));
            Console.Write(" ");
        }

        public void PrintGameOver()
        {
            Console.BackgroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2);
            Console.Write("GAME OVER");
        }

        public void Beep()
        {
            Console.Beep();
        }

    }
}
